﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Module_Basic_Entities;

namespace Module_Persistence
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content
    /// Pipeline to read the specified data type from binary .xnb format.
    /// 
    /// Unlike the other Content Pipeline support classes, this should
    /// be a part of your main game project, and not the Content Pipeline
    /// Extension Library project.
    /// </summary>
    internal class MapContentReader : ContentTypeReader<Map2D>
    {
        protected override Map2D Read(Microsoft.Xna.Framework.Content.ContentReader input, Map2D existingInstance)
        {
            List<List<Tile>> tiles = null;
            List<Tile> helperTiles = null;
            List<BLKSTR> blocks = null;
            Tile tile;
            int countBLocks;
            
            int numLayers = 0, curLayer = 0;
            
            Map2D map = new Map2D();
            
            map.Init();

            Layer2D layer2D = null;

            do
            {
                tiles = new List<List<Tile>>();
                helperTiles = new List<Tile>();
                blocks = new List<BLKSTR>();

                numLayers = input.ReadObject<int>();

                countBLocks = input.ReadObject<int>();

                for (int i = 0; i < countBLocks; i++)
                {
                    BLKSTR block =
                        input.ReadObject<BLKSTR>(new BLKSTRCustomContentTypeReader(typeof(BLKSTR)));

                    blocks.Add(block);
                }

                int mapWidth = input.ReadObject<int>();
                int mapHeight = input.ReadObject<int>();
                int tileWidth = input.ReadObject<int>();
                int tileHeight = input.ReadObject<int>();

                Texture2D texture = input.ReadExternalReference<Texture2D>();

                for (int i = 0, j = 0, k = 0; i < (mapWidth * mapHeight); i++, j++)
                {
                    tile = input.ReadObject<Tile>(new TileCustomContentTypeReader(typeof(Tile)));

                    tile.Position = new Vector2(k * tileHeight, j * tileWidth);

                    helperTiles.Add(tile);

                    if ((mapHeight * k + j) >= (k * mapHeight + mapHeight - 1))
                    {
                        k++;
                        j = -1;
                        tiles.Add(helperTiles);
                        helperTiles = new List<Tile>();
                    }
                }
                
                layer2D = new Layer2D(tiles, mapWidth, mapHeight, tileWidth, tileHeight, blocks);                

                map.Add(layer2D);

                int tileRows = input.ReadObject<int>();
                int tileColumns = input.ReadObject<int>();
                int totalTiles = input.ReadObject<int>();

                layer2D.TileStripTexture = texture;

                layer2D.BuildSourceRectanglesTileStrip(tileRows, tileColumns);

                curLayer++;

            } while (curLayer < numLayers);

            return map;
        }
    }
}
